from enum import Enum


class ScanProfile(Enum):
    """enum ScanProfile {
        SP_DEFAULT = 0;
        SP_FULL = 1;
        SP_HIGH_RISK = 2;
        SP_SQL_INJECTION = 3;
        SP_XSS = 4;
        SP_NORMAL = 5;
        SP_QUICK = 6;
        }

    Args:
        Enum ([type]): [description]
    """
    high = 2                     # 高危漏洞检测
    medium = 1                   # 全面深入检测
    low = 1                      # 全面深入检测
    file_upload = 14
    directory_listing = 11
    xss = 4
    directory_traversal = 12
    code_execution = 7
    csrf = 10
    file_inclusion = 13
    injection = 16
    configuration = 9
    information_disclosure = 15
    command_execution = 8
    malware = 17
    webvul = 18


class NotifyType(Enum):
    """ NT_TASK_SCHEDULED  // 任务创建成功进入调度
        NT_TASK_QUEUED  // 任务到达应当执行的时间点进入执行队列
        NT_TASK_PROCESSING  // 任务开始实际执行
        NT_TASK_COMPLETED  // 任务执行完成
        NT_TASK_ABORTED  // 任务被中止
        NT_TASK_FAILED  // 任务执行失败
        NT_TASK_REMOVED  // 任务被删除
        NT_PUSH_EVENTS  // event 结果推送
        NT_PUSH_URLS  // Urls 结果推送

        'waiting', // 等待执行
        'active', // 正在执行
        'completed', // 完成
        'failed', // 失败
        'stopped', // 手动终止

    Args:
        Enum ([type]): [description]
    """
    NT_TASK_QUEUED = 'waiting'
    NT_TASK_PROCESSING = 'active'
    NT_TASK_COMPLETED = 'completed'
    NT_TASK_FAILED = 'failed'
    NT_TASK_ABORTED = 'stopped'


# 请求数据对应task表的字段
# request_data_key: task_key
TASK_UPDATE_KEY_MAP = {
    'task_id': 'taskId',
    'task_session_id': 'taskSessionId',
    'task.progress': 'progress',
    'task.next_start_at': 'nextExecDate',
    'task.addition': '*',
    'task.proxy_ip': "proxyIp"
}


class TaskStatus(Enum):
    waiting = 'waiting'
    active = 'active'
    completed = 'completed'
    aborted = 'aborted'
    failed = 'failed'
    stalled = 'stalled'
    stopped = 'stopped'


class LimitType(Enum):
    frequency = 'frequency'
    period = 'period'
    limit = 'limit'
    duration = 'duration'
    max = 'max'
    exp = 'exp'  # 限制到期时间


class ChargeType(Enum):
    plan = 'plan'
    addition_package = 'additionPackage'


class AssetType(Enum):
    host = "host"
    web = "web"


class TaskTriggerType(Enum):
    manual = 'manual'
    schedule = 'schedule'
    scheduleIncrement = 'scheduleIncrement'


class AssetTaskGroup(Enum):
    host = "host"
    web_security = "web_security"
    web_asset = "web_asset"
    http = "http"
    ping = "ping"
    ipv6 = "ipv6"


class TaskType(Enum):
    vul = 'vul'
    ssl = 'ssl'
    asset = 'asset'
    security_event = 'securityEvent'
    content = 'content'
    site_portrait = 'sitePortrait'
    ping = 'ping'
    http = 'http'
    hostVul = 'hostVul'
    ipv6 = 'ipv6'
    change_check = "changeCheck"


TASK_TYPE_CONFIG = {
    TaskType.vul.value: {
        "cn": "Web漏洞监测",
        "en": TaskType.vul.value,
        "line": "vul",
        "at_group": AssetTaskGroup.web_security.value,
        "sub_group": "weakness",
        "trigger": [TaskTriggerType.manual.value, TaskTriggerType.schedule.value],
        "is_allow_alert": True,
        "is_allow_api_push": True
    },
    TaskType.ssl.value: {
        "cn": "SSL安全监测",
        "en": TaskType.ssl.value,
        "line": "ssl",
        "at_group": AssetTaskGroup.web_security.value,
        "sub_group": "weakness",
        "trigger": [TaskTriggerType.manual.value, TaskTriggerType.schedule.value],
        "is_allow_alert": True,
        "is_allow_api_push": True
    },
    TaskType.asset.value: {
        "cn": "资产变动监测",
        "en": TaskType.asset.value,
        "line": "asset",
        "at_group": AssetTaskGroup.web_asset.value,
        "sub_group": "asset",
        "trigger": [TaskTriggerType.schedule.value, ],
        "is_allow_alert": True,
        "is_allow_api_push": True
    },
    TaskType.content.value: {
        "cn": "内容违规监测",
        "en": TaskType.content.value,
        "line": "content",
        "at_group": AssetTaskGroup.web_security.value,
        "sub_group": "content",
        "trigger": [TaskTriggerType.manual.value, TaskTriggerType.schedule.value],
        "is_allow_alert": True,
        "is_allow_api_push": True
    },
    TaskType.security_event.value: {
        "cn": "安全事件监测",
        "en": TaskType.security_event.value,
        "line": "security_event",
        "at_group": AssetTaskGroup.web_security.value,
        "sub_group": "security_event",
        "trigger": [TaskTriggerType.manual.value, TaskTriggerType.schedule.value],
        "is_allow_alert": True,
        "is_allow_api_push": True
    },
    TaskType.site_portrait.value: {
        "cn": "网站画像监测",
        "en": TaskType.site_portrait.value,
        "line": "site_portrait",
        "at_group": "site_portrait",
        "sub_group": "site_portrait",
        "trigger": [TaskTriggerType.schedule.value, ],
        "is_allow_alert": False,
        "is_allow_api_push": False
    },
    TaskType.ping.value: {
        "cn": "Ping监测",
        "en": TaskType.ping.value,
        "line": "ping",
        "at_group": AssetTaskGroup.ping.value,
        "sub_group": "available",
        "trigger": [TaskTriggerType.schedule.value, ],
        "is_allow_alert": True,
        "is_allow_api_push": True
    },
    TaskType.http.value: {
        "cn": "HTTP监测",
        "en": TaskType.http.value,
        "line": "http",
        "at_group": AssetTaskGroup.http.value,
        "sub_group": "available",
        "trigger": [TaskTriggerType.schedule.value, ],
        "is_allow_alert": True,
        "is_allow_api_push": True
    },
    TaskType.ipv6.value: {
        "cn": "IPv6合规监测",
        "en": TaskType.ipv6.value,
        "line": "ipv6",
        "at_group": AssetTaskGroup.ipv6.value,
        "sub_group": "ipv6",
        "trigger": [TaskTriggerType.manual.value, ],
        "is_allow_alert": False,
        "is_allow_api_push": True
    },
    TaskType.hostVul.value: {
        "cn": "主机漏洞监测",
        "en": TaskType.hostVul.value,
        "line": "host_vul",
        "at_group": AssetTaskGroup.host.value,
        "sub_group": "host_vul",
        "trigger": [TaskTriggerType.schedule.value, TaskTriggerType.manual.value],
        "is_allow_alert": True,
        "is_allow_api_push": True
    },
    TaskType.change_check.value: {
        "cn": "篡改监测",
        "en": TaskType.change_check.value,
        "line": "change_check",
        "at_group": AssetTaskGroup.web_security.value,
        "sub_group": "change_check",
        "trigger": [TaskTriggerType.schedule.value, ],
        "is_allow_alert": True,
        "is_allow_api_push": True
    },
}


class ExTaskType(Enum):
    change_check_config = "changeCheckConfig"   # 手动更新篡改监测基准


class ChangeCheckType(Enum):
    text = "text"
    structure = "structure"
    resource = "resource"


class ForeignLinksPlugin(Enum):
    foreign_links_1 = 'foreign_links_1'
    foreign_links_2 = 'foreign_links_2'
    foreign_links_3 = 'foreign_links_3'


class SecurityEventPlugin(Enum):
    black_links = 'black_links'
    broken_links = 'broken_links'
    cryjack = 'cryjack'
    malscan = 'malscan'
    foreign_links = "foreign_links"


class ContentPlugin(Enum):
    keyword = "keyword"
    privacy_disclosure = "privacy_disclosure"


class VulType(Enum):
    full = 'full'
    increment = 'increment'


class ScanJobName(Enum):
    site_info = 'site_info'
    appscan = 'appscan'
    kscan = 'kscan'
    nscan = 'nscan'
    pinpin = 'network_scan'


class NetWorkType(Enum):
    v4 = 1
    v6 = 2
    v4v6 = 3


class PreLoginMethod(Enum):
    form = 'form'
    json = 'json'
    cookie = 'cookie'
    headers = 'headers'


class DateRange(Enum):
    last = 'last'
    prev = 'prev'
    range = 'range'


CHANGE_CHECK_TYPE_TUPLE = tuple(t.value for t in ChangeCheckType)
EX_TASK_TYPES = tuple(t.value for t in ExTaskType)
ASSET_TASK_GROUP_TUPLE = tuple(item.value for item in AssetTaskGroup)
WEAKNESS_TASK_GROUP_TUPLE = tuple(v["en"] for v in TASK_TYPE_CONFIG.values() if v["sub_group"] == "weakness")
WS_TASK_GROUP_TUPLE = tuple(v["en"] for v in TASK_TYPE_CONFIG.values() if v["at_group"] == AssetTaskGroup.web_security.value)
AVAILABLE_TASK_GROUP_TUPLE = tuple(v["en"] for v in TASK_TYPE_CONFIG.values() if v["sub_group"] == "available")
TASK_TYPE_TUPLE = tuple(item.value for item in TaskType)
MONITOR_TYPE_TUPLE = tuple(item.value for item in TaskType if item not in [TaskType.site_portrait, TaskType.ipv6, TaskType.hostVul])
FOREIGN_LINKS_PLUGIN_TUPLE = tuple(item.value for item in ForeignLinksPlugin)
SECURITY_EVENT_PLUGIN_TUPLE = tuple(item.value for item in SecurityEventPlugin)
SECURITY_EVENT_PLUGIN_FOREIGN_LINKS_TUPLE = tuple(item.value for item in SecurityEventPlugin if item.value != "foreign_links") + FOREIGN_LINKS_PLUGIN_TUPLE
CONTENT_PLUGIN_TUPLE = tuple(item.value for item in ContentPlugin)
ENABLED_FUNC_TYPE = ('domain', 'httpMaxTask', 'pingMaxTask')


class NewsType(Enum):
    product = 'product'
    vul = 'vul'
    service = 'service'


class NetworkScanProfile(Enum):
    SP_DEFAULT = 0
    SP_LOW_RISK = 1
    SP_MID_RISK = 2
    SP_HIGH_RISK = 3
    SP_FULL = 4


TriggerTypeMap = {"schedule": "周期性", "manual": "单次"}


HOST_TYPE = {
    "general purpose": "一般用途",
    "bridge": "网桥",
    "broadband router": "宽带路由器",
    "firewall": "防火墙",
    "game console": "游戏机",
    "hub": "集线器",
    "load balancer": "负载均衡器",
    "media device": "媒体设备",
    "PBX": "私人小交换机",
    "PDA": "掌上电脑",
    "phone": "电话",
    "power-device": "电源设备",
    "printer": "打印机",
    "print server": "打印服务器",
    "proxy server": "代理服务器",
    "remote management": "远程管理设备",
    "router": "路由器",
    "security-misc": "其他安全设备",
    "specialized": "专门",
    "storage-misc": "存储设备",
    "switch": "交换机",
    "telecom-misc": "电信杂项",
    "terminal": "终端",
    "terminal server": "终端服务器",
    "VoIP adapter": "网络电话适配器",
    "VoIP phone": "网络电话",
    "WAP": "无线接入点",
    "webcam": "摄像头"
}


class InspectionEventMarkerStatus(Enum):
    UN_MARKED = 0
    IGNORE = 1
    FALSE_POSITIVE = 2
    CONFIRM = 3
    RESET = 4


class InspectionStrategyIgnoreType(Enum):
    IGNORED = 1
    FALSE_POSITIVE = 2


class InspectionStrategySwitchStatus(Enum):
    OPEN = 1
    CLOSE = 0


class NotificationTarget(Enum):
    SCANV = 1
    EASM = 2


class NotificationTargetQueue(Enum):
    SCANV = "notify_scanv"
    EASM = "notify_easm"


class TaskName(Enum):
    LOAD_SCAN_EVENTS_FOR_SCANV = "load_scan_events_for_scanv"
    LOAD_SCAN_EVENTS_FOR_EASM = "load_scan_events_for_easm"


class ScanSpeed(Enum):
    AUTO = 0
    VERY_LOW = 1
    LOW = 2
    MEDIUM = 3
    HIGH = 4


SPEED_CONCURRENCY_MAP = {
    0: 100,
    1: 30,
    2: 60,
    3: 120,
    4: 240
}
